﻿using System;
using System.ComponentModel;

namespace UtfprLibrary.EntityFramework.ComponentModel {
    /// <summary>
    /// Custom PropertyDescriptor used by the EntitySetTypeDescriptor (above)
    /// to expose EntitySets as properties.
    /// </summary>
    /// <remarks>
    /// Reflected from DataTablePropertyDescriptor
    /// </remarks>
    class EntitySetPropertyDescriptor : PropertyDescriptor {
        #region Fields

        EntitySet _view;

        #endregion

        #region Constructor

        internal EntitySetPropertyDescriptor(EntitySet view)
            : base(view.Name, null) {
            _view = view;
        }

        #endregion

        #region Overrides

        public override bool CanResetValue(object component) { return false; }

        public override bool Equals(object other) {
            if (other is EntitySetPropertyDescriptor) {
                EntitySetPropertyDescriptor descriptor = (EntitySetPropertyDescriptor)other;
                return (descriptor._view == this._view);
            }
            return false;
        }

        public override int GetHashCode() {
            return this._view.GetHashCode();
        }

        public override object GetValue(object component) {
            return _view.List;
        }

        public override void ResetValue(object component) { }

        public override void SetValue(object component, object value) { }

        public override bool ShouldSerializeValue(object component) {
            return false;
        }

        public override Type ComponentType {
            get { return typeof(EntitySet); }
        }

        public override bool IsReadOnly {
            get { return false; }
        }

        public override Type PropertyType {
            get { return typeof(IBindingList); }
        }

        #endregion
    }
}
